﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using TVersityDotNet;

namespace TVersityDotNetTests.Mocks
{
    /// <summary>
    /// IMessageManager implementation for testing
    /// </summary>
    class TestMessageManager : IMessageManager
    {
		/// <summary>
		/// Ctor with no initializers
		/// </summary>
		public TestMessageManager()
		{
		}

		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="isSuccessCase">Indicates whether the message manager represents a success case or not</param>
		public TestMessageManager(bool isSuccessCase)
		{
			_directory = GenerateDirectory(isSuccessCase);
		}

		private string _directory;

		/// <summary>
		/// Use this method to set up the type of test case the message manager will be running
		/// </summary>
		/// <param name="isSuccessCase">Indicates whether the message manager represents a success case or not</param>
		public void SetTestCase(bool isSuccessCase)
		{
			_directory = GenerateDirectory(isSuccessCase);
		}

		private string GenerateDirectory(bool isSuccessCase)
		{
			return isSuccessCase ? "APIResponseSuccessCases" : "APIResponseFailureCases";
		}

        /// <summary>
        /// Represents the address of the API
        /// </summary>
        public Uri BaseUrl { get; set; }

        /// <summary>
        /// Process a mock request
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public TVersityResponse ProcessRequest(TVersityRequest request)
        {
			string path = Path.Combine(_directory, GenerateFilename(request.Path));
			return request.CreateResponse(XDocument.Load(new StreamReader(path)));
        }

        private string GenerateFilename(string resourcePath)
        {
            return string.Format("{0}.xml", resourcePath.Replace("/", string.Empty));
        }
    }
}
